package cn.cocho.custom.yunCashierDf.utils.db;

import cn.cocho.common.web.db.DBConstants;
import cn.cocho.custom.yunCashierDf.cache.CacheCtrl;
import cn.cocho.custom.yunCashierDf.utils.AppContexts;
import cn.cocho.custom.yunCashierDf.utils.LoggerUtils;
import cn.cocho.dborm.core.Cache;
import cn.cocho.dborm.util.DbormDataBase;
import cn.cocho.dborm.util.ReflectUtilsDborm;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

;

public class DataBaseManager extends DbormDataBase {

    @Autowired
    LoggerUtils logger;

    @Autowired
    CacheCtrl cacheCtrl;

    private DataSource dataSource;

    /**
     * 获得数据库连接
     *
     * @return
     * @author FIRST WIT
     */
    public Connection getConnection() {
        try {
            return getDataSource().getConnection();
        } catch (SQLException e) {
            logger.error(e);
        }
        return null;
    }


    @Override
    public <T> T beforeInsert(T entity) {
        super.beforeInsert(entity);
//        UcUserInfo ucUserInfo = cacheCtrl.getTokenCtrl().getCurrentUcUserInfo();
        Map<String, Field> fields = Cache.getEntityAllFieldsCache(entity.getClass());
        if (AppContexts.getUserId() != null) {
            Object corpId = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.CORP_ID), entity);
            if (corpId == null) {
                ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.CORP_ID), entity, AppContexts.getCorpId());
            }
            Object createUserId = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.CREATE_USER_ID), entity);
            if (createUserId == null) {
                ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.CREATE_USER_ID), entity, AppContexts.getUserId());
            }
            Object lastModifyUserId = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.LAST_MODIFY_USER_ID), entity);
            if (lastModifyUserId == null) {
                ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.LAST_MODIFY_USER_ID), entity, AppContexts.getUserId());
            }
        }
        long currentTime = new Date().getTime();

        Object createTime = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.CREATE_TIME), entity);
        if (createTime == null) {
            ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.CREATE_TIME), entity, currentTime);
        }
        Object lastModifyTime = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.LAST_MODIFY_TIME), entity);
        if (lastModifyTime == null) {
            ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.LAST_MODIFY_TIME), entity, currentTime);
        }
        ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.RECORD_STATUS), entity, DBConstants.RECORD_STATUS_INSERT);
        return entity;
    }

    @Override
    public <T> T beforeUpdate(T entity) {
        super.beforeUpdate(entity);
//        UcUserInfo ucUserInfo = cacheCtrl.getTokenCtrl().getCurrentUcUserInfo();
        Map<String, Field> fields = Cache.getEntityAllFieldsCache(entity.getClass());
        if (AppContexts.getUserId() != null) {
            Object corpId = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.CORP_ID), entity);
            if (corpId == null) {
                ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.CORP_ID), entity, AppContexts.getCorpId());
            }
            Object lastModifyUserId = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.LAST_MODIFY_USER_ID), entity);
            if (lastModifyUserId == null) {
                ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.LAST_MODIFY_USER_ID), entity, AppContexts.getUserId());
            }
        }
        Object lastModifyTime = ReflectUtilsDborm.getFieldValue(fields.get(DBConstants.LAST_MODIFY_TIME), entity);
        if (lastModifyTime == null) {
            ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.LAST_MODIFY_TIME), entity, new Date().getTime());
        }
        ReflectUtilsDborm.setFieldValue(fields.get(DBConstants.RECORD_STATUS), entity, DBConstants.RECORD_STATUS_UPDATE);
        return entity;
    }

    @Override
    public <T> T beforeReplace(T entity) {
        super.beforeReplace(entity);
        entity = beforeUpdate(entity);
        return entity;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
